<?php
declare(strict_types=1);

namespace App\Console\Commands\Tasks;


use App\Enums\GroupEnum;
use App\Repositories\Group\GroupCategoryRepository;
use App\Repositories\Group\GroupMemberRepository;
use App\Repositories\Group\GroupRepository;
use App\Repositories\Group\Message\GroupMemberNameRepository;
use Illuminate\Console\Command;

class GroupMemberNameCommand extends Command
{
    //命令名
    protected $signature = 'task:update_group_member_name';

    //命令描述:用户充值太过频繁报警
    protected $description = 'examine';

    public function __construct()
    {
        parent::__construct();
    }

    public function getSignature(): string
    {
        return $this->signature;
    }

    //业务处理
    public function handle()
    {
        // 获取交友群分类
        $categoryId = GroupCategoryRepository::getInstance()->value(["type" => GroupEnum::GROUP_CATEGORY_TYPE_FRIEND]);
        // 获取交友群
        $groupIds = GroupRepository::getInstance()->value(["category_id" => $categoryId]);

        //安群取昵称 防止不够用 群内昵称不重复就行
        foreach ($groupIds as $groupId){
            // 成员
            $members = GroupMemberRepository::getInstance()->get(["group_id" =>[ $groupId]]);
            // 所有昵称
            $names = GroupMemberNameRepository::getInstance()->get(["is_visible" => 1])->pluck('name')->shuffle()->toArray();

            foreach ($members as $member){
                if(count($names) <= 0){
                    $name = GroupMemberNameRepository::getInstance()->generateSystemName();
                }else {
                    $name = array_pop($names);
                }
                GroupMemberRepository::getInstance()->update($member->id, ["nickname" => $name]);
            }
        }
    }


}